#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 35;

int n, m;
int s[N], t[N];
int a[N][N];

int select(int i) {
  int mx = i;
  rep(j, i + 1, n) if (a[j][i] > a[mx][i]) mx = j;
  return mx;
}
void swap_row(int i, int j) { rep(k, 1, n + 1) swap(a[i][k], a[j][k]); }
void sub(int k, int i) {
  if (k == i || !a[k][i]) return;
  rep(j, 1, n + 1) a[k][j] ^= a[i][j];
}
int gauss() {
  rep(i, 1, n) {
    int j = select(i);
    if (j != i) swap_row(i, j);
    if (!a[i][i]) continue;
    rep(k, 1, n) sub(k, i);
  }
  //
  rep(i, 1, n) {
    int cnt = 0;
    rep(j, 1, n) cnt += a[i][j];
    if (!cnt && a[i][n + 1]) return 0;
  }
  int ans = 1;
  rep(i, 1, n) if (!a[i][i]) ans *= 2;
  return ans;
}
void solve() {
  memset(a, 0, sizeof(a));
  cin >> n;
  rep(i, 1, n) cin >> s[i];
  rep(i, 1, n) cin >> t[i];
  rep(i, 1, n) a[i][n + 1] = s[i] ^ t[i];
  int x, y;
  while (cin >> x >> y) {
    if (!x && !y) break;
    a[y][x] = 1;
  }
  rep(i, 1, n) a[i][i] = 1;
  int res = gauss();
  if (res <= 0)
    cout << "Oh,it's impossible~!!" << endl;
  else
    cout << res << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}